#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    A=new martix(1,1);
    B=nullptr;
    indexi=0;
    indexj=0;
    targetB=false;

    //限制输入内容
    setLimit(ui->numerator);
    setLimit(ui->denominator);
    setLimit(ui->numerator2);
    setLimit(ui->denominator2);

}

void MainWindow::keyPressEvent(QKeyEvent* event)
{

    if(event->key()==Qt::Key_Enter || event->key()==Qt::Key_Return)
        on_pushButton_5_clicked();
}
void MainWindow::setLimit(QLineEdit* edit)
{
    QRegExp regx("^-?\\d+$");
    QValidator *validator = new QRegExpValidator(regx, edit );
    edit->setValidator(validator);
}


MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_5_clicked()
{
    if(targetB==false){
        if(indexi>=A->getm())
            return;
    int nume=0;
    int deno=0;
    if(ui->numerator->text()=="")
        return;

    nume=ui->numerator->text().toInt();
    if(ui->denominator->text()=="")
        deno=1;
    else
        deno=ui->denominator->text().toInt();
    if(deno==0)
        return;
    fraction t(nume,deno);
    A->element[indexi][indexj]=t;
    ui->textBrowser->clear();
    printA();
    indexj++;
    if(indexj>=A->getn())
    {
        indexj=0;
        indexi++;
    }
}
    else{
        if(indexi>=B->getm())
            return;
        int nume=0;
        int deno=0;
        if(ui->numerator->text()=="")
            return;

        nume=ui->numerator->text().toInt();
        if(ui->denominator->text()=="")
            deno=1;
        else
            deno=ui->denominator->text().toInt();
        if(deno==0)
            return;
        fraction t(nume,deno);
        B->element[indexi][indexj]=t;
        ui->textBrowser->clear();
        printB();
        indexj++;
        if(indexj>=B->getn())
        {
            indexj=0;
            indexi++;
        }
    }
}

void MainWindow::on_comboBox_currentIndexChanged(int index)
{
    //（重新）创建矩阵A
    if(targetB==false){
        ui->textBrowser->clear();
    if(A!=nullptr)
        delete A;
    int ma=ui->comboBox->currentIndex()+1;
    int na=ui->comboBox_2->currentIndex()+1;
    A = new martix(ma,na);
    resetij();
}
    else{
        ui->textBrowser->clear();
        printC();
    if(B!=nullptr)
        delete B;
    int ma=ui->comboBox->currentIndex()+1;
    int na=ui->comboBox_2->currentIndex()+1;
    B = new martix(ma,na);
    resetij();
    }
}

void MainWindow::on_comboBox_2_currentIndexChanged(int index)
{
    //（重新）创建矩阵A
    if(targetB==false){
        ui->textBrowser->clear();
    if(A!=nullptr)
        delete A;
    int ma=ui->comboBox->currentIndex()+1;
    int na=ui->comboBox_2->currentIndex()+1;
    A = new martix(ma,na);
    resetij();
}
    else{
        ui->textBrowser->clear();
        printC();
    if(B!=nullptr)
        delete B;
    int ma=ui->comboBox->currentIndex()+1;
    int na=ui->comboBox_2->currentIndex()+1;
    B = new martix(ma,na);
    resetij();
    }
}

void MainWindow::on_pushButton_clicked()
{
    ui->textBrowser->append(A->det().print());
}

void MainWindow::on_pushButton_4_clicked()
{
    ui->textBrowser->append(A->inv().print());
}
void MainWindow::printA(){
    if(indexi!=-1){
    QString t="";
    for(int i=0;i<indexi;i++){
        for(int j=0;j<A->getn();j++)
            t+=" "+A->element[i][j].print();
        t+="\n";
    }
        for(int j=0;j<=indexj;j++)
            t+=" "+A->element[indexi][j].print();
        ui->textBrowser->append(t);
    }
};

void MainWindow::on_pushButton_9_clicked()
{
    ui->numerator->clear();
    ui->denominator->clear();
}

void MainWindow::on_pushButton_10_clicked()
{
    ui->textBrowser->clear();
    if(indexj==0){
        indexj=A->getn()-1;
        indexi--;
    }
    else
        indexj--;
    A->element[indexi][indexj]=0;
    if(indexj==0){
        indexj=A->getn()-1;
        indexi--;
    }
    else
        indexj--;
    printA();
    indexj++;
    if(indexj>=A->getn())
    {
        indexj=0;
        indexi++;
    }
}

void MainWindow::on_pushButton_3_clicked()
{
    ui->textBrowser->clear();
    indexi=0;
    indexj=0;
    if(targetB==false){
    int ma=ui->comboBox->currentIndex()+1;
    int na=ui->comboBox_2->currentIndex()+1;
    A = new martix(ma,na);
    }
    else{
        int ma=ui->comboBox->currentIndex()+1;
        int na=ui->comboBox_2->currentIndex()+1;
        B = new martix(ma,na);
        printC();
    }
}

void MainWindow::on_pushButton_11_clicked()
{
    ui->numerator->clear();
    ui->denominator->clear();
    indexi=0;
    indexj=0;
    int ma=ui->comboBox->currentIndex()+1;
    int na=ui->comboBox_2->currentIndex()+1;
    B=new martix(ma,na);
    targetB=true;
}
void MainWindow::printB(){
    QString t="";
    for(int i=0;i<A->getm();i++){
        for(int j=0;j<A->getn();j++)
            t+=" "+A->element[i][j].print();
        t+="\n";
    }
    t+="\n";
    if(indexi!=-1){
    for(int i=0;i<indexi;i++){
        for(int j=0;j<B->getn();j++)
            t+=" "+B->element[i][j].print();
        t+="\n";
    }
        for(int j=0;j<=indexj;j++)
            t+=" "+B->element[indexi][j].print();
        ui->textBrowser->append(t);
    }
}

void MainWindow::on_pushButton_7_clicked()
{
    if(targetB==false)
        return;
    ui->textBrowser->append((*A+*B).print());
}

void MainWindow::on_pushButton_6_clicked()
{
    if(targetB==false)
        return;
    ui->textBrowser->append((*A-*B).print());
}

void MainWindow::on_pushButton_2_clicked()
{
    if(targetB==false)
        return;
    ui->textBrowser->append((*A**B).print());
}

void MainWindow::on_pushButton_8_clicked()
{

    int nume=0;
    int deno=0;
    if(ui->numerator2->text()=="")
        return;

    nume=ui->numerator2->text().toInt();
    if(ui->denominator2->text()=="")
        deno=1;
    else
        deno=ui->denominator2->text().toInt();
    if(deno==0)
        return;
    fraction t(nume,deno);
    ui->textBrowser->append((t**A).print());
}

void MainWindow::on_pushButton_12_clicked()
{
    targetB=false;
}

void MainWindow::on_pushButton_13_clicked()
{
    ui->textBrowser->append((*A**A).print());
}
void MainWindow::paintEvent(QPaintEvent* event)
{
    QPainter p(this);
    QRect r=rect();
    QImage bg(":/bg.jpg");
    p.drawImage(r,bg);
}
void MainWindow::printC(){
    QString t="";
    for(int i=0;i<A->getm();i++){
        for(int j=0;j<A->getn();j++)
            t+=" "+A->element[i][j].print();
        t+="\n";
    }
    ui->textBrowser->append(t);
}
